﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace LP_XML
{
    class Funciones
    {
        public static void writeXML(Empleado e)
        {
            //Save the XML Document to a XMLWriter or to the disk           
            XDocument xDoc = new XDocument(
                        new XDeclaration("1.0", "UTF-16", null),
                        new XElement("Empleados",
                            new XElement("Empleado",
                                new XElement("Nombre", e.nombre),
                                new XElement("Apellido", e.apellidos),
                                new XElement("Dni", e.dni),
                                new XElement("FechaNac", e.fechaNac),
                                new XElement("Sexo", e.sexo),
                                new XElement("Telefono", e.telefono),
                                new XElement("Email", e.email)
                                )));

            StringWriter sw = new StringWriter();
            XmlWriter xWrite = XmlWriter.Create(sw);
            xDoc.Save(xWrite);
            xWrite.Close();

            // Save to Disk
            xDoc.Save("C:\\Users\\Yanire\\Documents\\Visual Studio 2012\\Projects\\LP-XML\\Empleado.xml");
            Console.WriteLine("Saved");
        }

        public static void loadXML()
        {
            //Load an XML Document using XML Reader           
            XmlReader xRead = XmlReader.Create(@"C:\\Users\\Yanire\\Documents\\Visual Studio 2012\\Projects\\LP-XML\\Empleado.xml");
            XElement xEle = XElement.Load(xRead);
            Console.WriteLine(xEle);
            xRead.Close();
        }

        public static void addXML(Empleado e)
        {
            //Load an XML Document using XML Reader 
            string spath = "C:\\Users\\Yanire\\Documents\\Visual Studio 2012\\Projects\\LP-XML\\Empleado.xml";
            XDocument doc = XDocument.Load(spath);
            XElement root = new XElement("Empleado",
                                new XElement("Nombre", e.nombre),
                                new XElement("Apellido", e.apellidos),
                                new XElement("Dni", e.dni),
                                new XElement("FechaNac", e.fechaNac),
                                new XElement("Sexo", e.sexo),
                                new XElement("Telefono", e.telefono),
                                new XElement("Email", e.email)
                                );
            doc.Element("Empleados").Add(root);
            doc.Save(spath);
        }





    }
}
